package com.cskaoyan.mall.controller.mall;

import com.cskaoyan.mall.model.po.Type;
import com.cskaoyan.mall.model.vo.goods.GoodsMessageVo;
import com.cskaoyan.mall.model.vo.goods.GoodsTypeVo;
import com.cskaoyan.mall.service.lmpl.GoodsServiceImpl;
import com.cskaoyan.mall.utils.Result;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName: MallIndexServlet
 * @Description: TODO
 * @Auther 庄左
 * @Date 2022/7/3 17:38
 * @Version
 **/
@WebServlet("/api/mall/index/*")
public class MallIndexServlet extends HttpServlet {

    //获取json字符串
    private String getJsonString(HttpServletRequest req) throws IOException {
        ServletInputStream inputStream = req.getInputStream();
        //写入字节数组的输出流
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        int len = 0;
        byte[] bytes = new byte[1024];
        while ((len = inputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, len);
        }
        //转为字符串
        return outputStream.toString("utf-8");
    }
    private Gson gson = new Gson();
    private Result result =new Result();
    private GoodsServiceImpl goodsService = new GoodsServiceImpl();





    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String replace = requestURI.replace(req.getContextPath() + "/api/mall/index/", "");

        if ("getType".equals(replace)){
            getType(req,resp);
        }

    }



    private void getType(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<GoodsTypeVo> goodsTypeVo = goodsService.selectType();
        result.setCode(0);
        result.setData(goodsTypeVo);
        resp.getWriter().println(gson.toJson(result));
    }
}
